﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLCHBANGIAY.DATA
{
   public class BrandsController
    {
        Database db = DatabaseFactory.CreateDatabase();
        #region[Brand_GetById]
        public List<Brand> Brand_GetById(string Id)
        {
            List<Brand> list = new List<Brand>();
            Brand obj = new Brand();
            DbCommand cmd = db.GetStoredProcCommand("sp_Brands_GetById", Id);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.BrandsIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Brands_GetByTop]
        public List<Brand> Brand_GetByTop(string Top, string Where, string Order)
        {
            List<Brand> list = new List<Brand>();
            Brand obj = new Brand();
            DbCommand cmd = db.GetStoredProcCommand("sp_Brands_GetByTop", Top, Where, Order);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.BrandsIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Brand_GetByAll]
        public List<Brand> Brand_GetByAll(string Lang)
        {

            List<Brand> list = new List<Brand>();
            Brand obj = new Brand();
            DbCommand cmd = db.GetStoredProcCommand("sp_Brands_GetByAll", Lang);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.BrandsIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Brand_Paging]
        public List<Brand> Brand_Paging(string CurentPage, string PageSize, string Lang)
        {

            List<Brand> list = new List<Brand>();
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Brands_Paging", CurentPage, PageSize, Lang))
            {
                try
                {
                    Brand obj = new Brand();
                    using (IDataReader dr = db.ExecuteReader(cmd))
                    {
                        while (dr.Read())
                        {
                            list.Add(obj.BrandsIDataReader(dr));
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
            return list;
        }
        #endregion
        #region[Brand_Insert]
        public bool Brand_Insert(Brand data)
        {

            using (DbCommand cmd = db.GetStoredProcCommand("sp_Brands_Insert"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                cmd.Parameters.Add(new SqlParameter("@Logo", data.Logo));
                cmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
            //Clear cache
           // System.Web.HttpContext.Current.Cache.Remove("Brands");
            return true;
        }
        #endregion
        #region[Brand_Update]
        public bool Brand_Update(Brand data)
        {
            using (DbCommand dbCmd = db.GetStoredProcCommand("sp_Brands_Update"))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Logo", data.Logo));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                try
                {
                    db.ExecuteNonQuery(dbCmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (dbCmd != null) dbCmd.Dispose();
                }
            }
            //Clear cache
            //System.Web.HttpContext.Current.Cache.Remove("Brands");
            return true;
        }
        #endregion
        #region[Brand_Delete]
        public bool Brand_Delete(string Id)
        {
            using (DbCommand dbCmd = db.GetStoredProcCommand("sp_Brands_Delete"))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                try
                {
                    db.ExecuteNonQuery(dbCmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (dbCmd != null) dbCmd.Dispose();
                }
            }
            //Clear cache
        //    System.Web.HttpContext.Current.Cache.Remove("Brands");
            return true;
        }
        #endregion
    }
}
